package com.spzx.payment.receiver;

import com.rabbitmq.client.Channel;
import com.spzx.common.rabbit.constant.MqConst;
import com.spzx.order.api.domain.OrderInfo;
import com.spzx.payment.service.IAlipayService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.io.IOException;

/**
 * @Author ChenXing
 * @Description   支付模块消息监听
 * @Create 2024/8/19 19:29
 */

@Slf4j
@Component
public class PaymentReceiver {

    @Autowired
    private IAlipayService alipayService;

    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_PAYMENT_PAY, durable = "true"),
            value = @Queue(value = MqConst.QUEUE_PAYMENT_CLOSE, durable = "true"),
            key = {MqConst.ROUTING_PAYMENT_CLOSE}
    ))
    public void closePayment(String orderNo, Channel channel, Message message) {
        Assert.hasText(orderNo,"订单异常");
        try {

            // 核心业务，关闭支付
            alipayService.closePayment(orderNo);

            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
        }
    }




}
